var app = getApp() 
var model = require('../../utils/model/model.js');   
const util = require('../../utils/util.js')
Page({
  data:{
    imageUrl: app.globalData.imageUrl,
    flag: false,
    showModal: false,
    // hasUserInfo:false,
    userInfo: '',
    modelData:{
      showModal:true,
      cancelText:'不允许',
      confirmText:"允许",
      content:'“港悦生活” 要获取你的地理位置，是否允许？',
      confirmCallback:'confirmCallback',
      cancelCallback:'cancelCallback',
      error: ''
    }
  },
  onLoad:function(){
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        wx.setStorageSync('location', res)
      },
      fail: function (res) {
      }
    })
    model.init.apply(this, ['modelData']);
    wx.hideNavigationBarLoading();
    var that=this;
    wx.login({
      success: function (res) {
        that.getOpenId(res.code);
      }
    });
    if(wx.getStorageSync('userInfo') != ''){
      this.getUserInfo()
    }
    var that=this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {

        }
      }
    })
    this.getIs()
  },
  getIs: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'menu/index',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if(res.data.success){
          _this.setData({
            error: res.data.error
          })
          if(res.data.error == '1'){
            wx.switchTab({
              url: '/pages/index/index',
            })
            // if (wx.getStorageSync("userInfo") != '') {
            //   wx.redirectTo({
            //     url: '/pages/brandBusiness/brandBusiness',
            //   })
            // }
            // else{
            //   wx.redirectTo({
            //     url: '/pages/login/login',
            //   })
            // }
          }
          else{
            if (wx.getStorageSync("userInfo") != '') {
              wx.redirectTo({
                url: '/pages/home/home',
              })
            }
          }
        }
      }
    })
  },
  //第一步：获取openid  
  getOpenId: function (code) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'login',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: { 'code': code },
      success: function (res) {
        app.globalData.openId = res.data.openId;
      }
    })
  },
  // 查询用户信息
  getUserInfo: function () {
    var _this = this;
    var user = JSON.parse(wx.getStorageSync('userInfo'));
    wx.request({
      url: app.globalData.httpApiCar + 'tGyUserInfo/getUserInfoDetailByPhone',
      data: {
        phone: user.phone
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.statusCode == '200'){
          res.data.data.birthday = util.formatDate(res.data.data.birthday);
          // res.data.user.id ='00000000616e314a01616ef58c1a0002';
          wx.setStorageSync('userInfo', JSON.stringify(res.data.data))
        }
        
      }
    })
  },
  openHome: function(){
    if (this.data.error != 1){
      wx.redirectTo({
        url: '/pages/home/home',
      })
    }
    else{
      wx.redirectTo({
        url: '/pages/index/index',
      })
    }
  },
  // 对话框确认按钮点击回调事件
  confirmCallback:function(){
    this.setData({
      flag: true
    })
  },
  cancelCallback:function(){
    this.setData({
      flag: false
    })
  }
})